package toDo.app;

import interfaces.ICreateToDoActivity;
import database.DatabaseFacade;
import android.os.Bundle;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import toDo.app.R;
import toDo.app.controllers.CreateToDoActivityClickController;
import toDo.app.models.ToDoListItem;
import toDo.app.models.User;

public class CreateToDoActivity extends ICreateToDoActivity{
	protected User user;
	protected Button backButton;
	protected Button createButton;
	protected EditText nameField;
	protected EditText descriptionField;
	protected EditText commentsField;
	protected EditText dateField;
	protected EditText locationField;
	protected AutoCompleteTextView categoryAutoField;
	protected CheckBox completedCheck;
	protected CreateToDoActivityClickController cController;
	protected int id;
	protected int date = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.createtodo);

		createButton = (Button) findViewById(R.id.createToDoAddButton);
		backButton = (Button) findViewById(R.id.createToDoBackButton);
		nameField = (EditText) findViewById(R.id.createToDoItemNameField);
		descriptionField = (EditText) findViewById(R.id.createToDoDescriptionField);
		commentsField = (EditText) findViewById(R.id.createToDoCommentsField);
		categoryAutoField = (AutoCompleteTextView) findViewById(R.id.createToDoCategoryAutoField);
		completedCheck = (CheckBox) findViewById(R.id.createToDoCompletedCheck);
		dateField = (EditText) findViewById(R.id.createToDoDateField);
		locationField = (EditText) findViewById(R.id.createToDoLocationField);
		
		dateField.setKeyListener(null);

		user = User.getUserFromIntent(getIntent());
		id = ToDoListItem.NEW;

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
				DatabaseFacade.getListTypes(this, user));
		categoryAutoField.setAdapter(adapter);
		
		cController = new CreateToDoActivityClickController(this);
		
		createButton.setOnClickListener(cController);
		backButton.setOnClickListener(cController);
		dateField.setOnClickListener(cController);
	}

	@Override
	public User getUser() {
		return user;
	}

	@Override
	public int getDate() {
		return date;
	}

	@Override
	public void setDate(int time) {
		date = time;
	}

	@Override
	public void setDateText(String text) {
		dateField.setText(text);
	}

	@Override
	public String getName() {
		return nameField.getText().toString();
	}

	@Override
	public String getDescription() {
		return descriptionField.getText().toString();
	}

	@Override
	public String getComments() {
		return commentsField.getText().toString();
	}

	@Override
	public String getCategory() {
		return categoryAutoField.getText().toString();
	}

	@Override
	public int getId() {
		return id;
	}

	@Override
	public String getLocation() {
		return locationField.getText().toString();
	}

	@Override
	public int getCompleted() {
		return completedCheck.isChecked() ? 1 : 0;
	}

}
